<?php

class BbddDao extends CI_Model {

	function __construct()
	{
		parent::__construct();
	}
	
	function getBasesDeDatos(){
		$query = $this->db->query("SHOW DATABASES");
		return $query->result_array();
	}
	
	function getTablasByBbdd($bbdd){
		$query=$this->db->query("SHOW TABLES FROM ".$bbdd);
		return $query->result_array();
	}
	
	function getInfoCamposTabla($bbdd,$tabla){
		$query=$this->db->query(" SHOW COLUMNS FROM ".$bbdd.".".$tabla);
		return $query->result_array();
	}
	
	function getNombreCamposTabla($bbdd,$tabla){
		$query=$this->db->query(" SHOW COLUMNS FROM ".$bbdd.".".$tabla);
		$returnValue = $query->result_array();
		$info = array();
		for($i=0;$i<count($returnValue);$i++){
			$info[$i]['campo'] = $returnValue[$i]['Field'];
			$info[$i]['ancho'] = filter_var($returnValue[$i]['Type'],FILTER_SANITIZE_NUMBER_INT);
		}
		return $info;
	}
	
	function getAllInfoTabla($bbdd,$tabla,$sort,$limit){
		$query=$this->db->query("SELECT * FROM ".$bbdd.".".$tabla." ".$sort." ".$limit);
		return $query->result_array();
	}
	
	function getAllInfoTablaPorBusqueda($bbdd,$tabla,$campo_busqueda,$valor_busqueda,$sort,$limit){
		$query = $this->db->query("SELECT * FROM ".$bbdd.".".$tabla." WHERE ".$campo_busqueda." LIKE '%".$valor_busqueda."%' ".$sort." ".$limit);
		return $query->result_array();
	}
	
	function countRegistrosPorBusqueda($bbdd,$tabla,$campo_busqueda,$valor_busqueda,$sort,$limit){
		$query = $this->db->query("SELECT count(*) as total FROM ".$bbdd.".".$tabla." WHERE ".$campo_busqueda." LIKE '%".$valor_busqueda."%' ".$sort." ".$limit);
		$result = $query->result_array();
		return $result[0]['total'];
	}
	
	function contarRegistrosTabla($bbdd,$tabla){
		$query=$this->db->query(" SELECT COUNT(*) as total FROM ".$bbdd.".".$tabla);
		$result = $query->result_array();
		return $result[0]['total'];
	}
	
	function getRegistroTabla($bbdd,$tabla,$id){
		$query=$this->db->query("SELECT * FROM ".$bbdd.".".$tabla." WHERE id=".$id);
		$returnValue = $query->result_array();
		return $returnValue[0];
	}
	
}

?>
